package vumt.radiocountries.services;

import java.io.IOException;

import vumt.radiocountries.PlayerActivity;
import vumt.radiocountries.util.Global;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;

public class ManagerRadio extends Service {
	MediaPlayer mp;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		mp = new MediaPlayer();
		if (!mp.isPlaying()) {
			startRadio();
		}
	}

	void startRadio() {
		try {
			mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
				@Override
				public void onPrepared(MediaPlayer arg0) {
					PlayerActivity.updateview();
					mp.start();
				}
			});
			mp.setDataSource(Global.currLinkRadio);
			if (Global.isLog)
				Log.d("currLinkRadio", Global.currLinkRadio);
			mp.prepareAsync();
		} catch (IOException e) {
			Log.d("loi_radio", e.toString());
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		try {
			PlayerActivity.updateview();
			mp.stop();
			mp.reset();
			mp.release();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
	}

}
